<template>
  <div class="comment-index">
    <div class="comment-container">
      <div class="comment-search">
        <el-input
            placeholder="请输入内容"
            v-model="params.key" >
          <i slot="prefix" class="el-input__icon el-icon-search"></i>
          <el-button  slot="append"  type="success" style="text-align: center;" @click="getcommentList">搜索</el-button>
        </el-input>
      </div>
    </div>
    <div class="discuss comment-container">
      <div class="nav">
        <ul>
          <li><a href="javascript:void(0)" :style="params.field=='like_sum'?'color: #25bb9b':''" @click="clickOrder('like_sum')">最多点赞</a> &nbsp;&nbsp;|</li>
          <li><a href="javascript:void(0)" :style="params.field=='create_time'?'color: #25bb9b':''" @click="clickOrder('create_time')">最新发表</a> &nbsp;&nbsp;|</li>
          <li><a href="javascript:void(0)" :style="params.field=='click'?'color: #25bb9b':''" @click="clickOrder('click')">最热</a> </li>
<!--          <li><a href="javascript:void(0)" :style="params.field=='my'?'color: #25bb9b':''" @click="clickOrder('my')">|&nbsp;&nbsp;我的</a></li>-->
        </ul>

        <el-button @click="$router.push({path:'/main/comment/save',query:$route.query})" slot="append"  type="success" style="padding: 11px 20px;float: right;text-align: center">
          {{ $route.query.fatherId?'发布题目/比赛相关':'我要发布' }}</el-button>
        <el-checkbox style="float: right;margin-bottom: 10px;margin-right: 25px;margin-top: 7px;" v-model="checked" @change="onlyMe">只看我的</el-checkbox>
      </div>

      <div v-if="total==0">
        <el-empty description="未有人发布相关讨论"></el-empty>
      </div>
      <div class="comment-text comment-container" v-for="item in commentList" :key="item.id">
        <img @click="$router.push('/main/comment/detail/'+item.id+'?lurl='+$route.fullPath)" class="consumer_pic" :src="'/api/user/download/'+getUser(item.userId).avatarId"/>
        <div class="infor">
          <div>
            <h4 >
              <a href="javascript:void(0)" @click="$router.push('/main/comment/detail/'+item.id+'?lurl='+$route.fullPath)" v-html="item.title "></a>
              <i class="top" v-if="item.weight>0">置顶</i>
            </h4>
          </div>


          <ul style="vertical-align: bottom;">
            <li class="consumer_name" v-html="getUser(item.userId).username"></li>
            <li style="font-size: 80%;margin-top: 3px;">{{ item.updateTime }}</li>
<!--            <li style="font-size: 80%;margin-top: 3px;" class="ago">1天前回复</li>-->
          </ul>
        </div>
        <ul>
          <li>回复 {{ item.replyCount }}</li>
          <li>赞 {{ item.likeSum }}</li>
          <li>浏览 {{ item.click }}</li>
        </ul>

      </div>

    </div>
    <!--分页-->
    <div class="page" v-show="total>params.size">
      <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="params.size"
          :page-sizes="[10,20,30,50,100]"
          @current-change="chg"
          @size-change="chgsize"
      >
      </el-pagination>
    </div>

  </div>
</template>

<script lang="ts">
import * as commentApi from '@/api/comment'
import {getEasyUserById} from '@/api/admin'
import {loginUser} from "@/utils/auth"
export default {
  name: 'commentIndex',
  data() {
    return {
      checked:false, //只看我的
      params:{
        page:1,
        size:10,
        field:'create_time',
        type:0,
      },
      userNames:{},
      total:0,
      commentList:[],
    };
  },
  methods: {
    onlyMe(){ //只看我的
      if(this.checked){
        this.params.userId = loginUser().id;
      }else this.params.userId = null;
      this.getcommentList();
    },
    //每页记录数发生改变时
    chgsize(pgsize) {
      //更改每页记录数
      this.params.size = pgsize;
      //从每一页开始查询
      this.params.page = 1;
      //重新查询
      this.getcommentList();
    },
    //当页数发生变化时执行
    chg(curpage) {
      //更新查询的页数
      this.params.page = curpage;
      //重新查询
      this.getcommentList();
    },
    clickOrder(field){ //设置排序字段
      this.params.field = field;
      this.params.order = "desc";
      this.getcommentList();
    },
    getcommentList(){
      commentApi.commentListSearch(this.params).then(async res=>{
        if(res.data.data){
          var tables = res.data.data.dataList;
          //获取用户名字
          for(var i = 0;i<tables.length;i++){
            // console.info(tables[i].userId)
            if(!this.userNames[tables[i].userId]){
              await getEasyUserById({"id":tables[i].userId}).then(res2=>{
                if(res2.data.data&&res2.data.data.username){
                  // console.info("success="+res2.data.data.id);
                  this.userNames[res2.data.data.id] =   res2.data.data;
                }else{
                  this.userNames[tables[i].userId].username =  '<span style="color:grey">未知用户</span>';
                }
              });
            }
          }
          this.commentList = tables;
          this.total = res.data.data.total;
        }
      })

    },
    getUser(userId){
      return this.userNames[userId];
    },
  },
  created() {
    Object.assign(this.params,this.$route.query);
    this.getcommentList();
  }


}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.comment-index * {
  padding: 0;
  margin: 0;
  list-style: none;
}

.comment-index button{
  border: none;
  margin: 0;
}

.comment-index .comment-container {
  width: 1000px;
  margin: 0 auto;
  overflow: hidden;
}


.comment-index .discuss {
  margin-top: 30px;
}

.comment-index .nav{
  height: 30px;
  vertical-align: center;
}

.comment-index .nav ul li{
  float: left;
  padding-left: 5px;
  padding-right: 5px;
  font-size: 15px;
  color: #999;
  border-left: 1px solid #eee;
}
.comment-index .nav ul li a{
  color: #999;
}
.comment-index .nav span{
  float: right;
  text-align: center;
  line-height: 30px;
  width: 66px;
  height: 30px;
  background-color: skyblue;
  color: #fff;
  border-radius: 2px;
}

.comment-index .comment-text {
  position: relative;
  margin-top: 30px;
  padding-bottom: 10px;
  float: left;
  border-bottom: 1px solid #999;
}

.comment-index .comment-text .consumer_pic {
  float: left;
  width: 80px;
  height: 80px;
  border-radius: 40px;
  margin-right: 10px;
  /*background-image: url(../../assets/logo.png);*/
  background-size: cover;

}

.comment-index .comment-text .infor {
  float: left;
  height: 80px;

}

.comment-index .comment-text .infor  h4 {
  position: relative;
  padding-right: 60px;
  font-weight: 400;
  margin-bottom: 20px;
  float: left;

}
.comment-index .comment-text .infor > div {
  overflow: hidden;

}
.comment-index .comment-text .infor .top,
.comment-index .jing{
  position: absolute;
  right: 25px;
  top: 0px;
  display: inline-block;
  width: 30px;
  height: 20px;
  text-align: center;
  line-height: 20px;
  font-style: normal;
  font-size: 13px;
  color: #fff;
  background-color: skyblue;
  border-radius: 5px;
}

.comment-index .comment-text .infor .jing {
  right: 0px;
  top: 0px;
  display: inline-block;
  width: 20px;
  background-color: red;
}


.comment-index .comment-text .infor ul li{
  float: left;
}

.comment-index .comment-text .infor ul li {
  margin-right: 10px;
  font-size: 15px;
  color: #999;
}

.comment-index .comment-text .infor .consumer_name {
  color: purple;
}

.comment-index .comment-text > ul {
  position: absolute;
  right: 0px;
  bottom: 20px;
  font-size: 15px;
  color: #999;
}

.comment-index .comment-text > ul li {
  float: left;
  padding: 0 10px;
  border-right: 1px solid #999;
}

.comment-index .comment-text > ul li:last-child{
  border-right: 0;
}
</style>
<style>
.comment-index .comment-search  .el-input-group__append{
  background-color: #1daa1d;
  color: white;
  width: 100px;
  text-align: center;
}
</style>
